package ar.com.mooral.activities;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.data.Foods;
import ar.com.mooral.data.Result;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.FoodPojo;


public class Alimentacion extends CrohnActivity {

	protected Foods foods;
	protected String digestion = "";
	private String digestionAnterior = null;
/*	protected static String ALIMENTACION_DIGESTION_FACIL 	= "0";
	protected static String ALIMENTACION_DIGESTION_MODERADA = "1";
	protected static String ALIMENTACION_DIGESTION_DIFICIL 	= "2";
	*/
	private BasePojo pojo;

	
    protected void onStart() {

		super.onStart();

		
		if (!CrohnContext.isPojoLoaded()) {
			FoodPojo newPojo = new FoodPojo();
			newPojo.setDate(CrohnContext.getDateTimeNow("dd/MM/yyyy"));
			CrohnContext.setPojo(newPojo);
		}
		pojo = CrohnContext.getPojo();
		
		
		((Button) findViewById(R.id.btnAlimentacionFecha)).setText(CrohnContext.getHumanDate(pojo.getDate()));
		((Button) findViewById(R.id.btnAlimentacionRegistrarComidas)).setText(pojo.getName());
		
			
		updateDigestionImages();
		
/*			
			// Initialize default values
	        ((TextView) findViewById(R.id.lblAlimentacionFecha)).setText(CrohnContext.getDateTime());
	        
	        TextView lblAlimentacionRegistrarComidas = (TextView) findViewById(R.id.lblAlimentacionRegistrarComidas);
	    	if (CrohnContext.isFoodLoaded()){
	    		lblAlimentacionRegistrarComidas.setText(CrohnContext.getFoodName());
	    	} else {
	    		lblAlimentacionRegistrarComidas.setText("");
	    	}
		}
*/		
	}
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alimentacion);
        
		foods = new Foods(this);
		
        // RETORNO
		TextView lblAlimentacionRetorno = (TextView) findViewById(R.id.lblAlimentacionRetorno);
        lblAlimentacionRetorno.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }
		});	        

		
        // REGISTRAR
        Button btnAlimentacionRegistrar = (Button) findViewById(R.id.btnAlimentacionRegistrar);
        btnAlimentacionRegistrar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {

		    	if (((FoodPojo)pojo).getDigestion() == null) {
		    		Result r = new Result(Result.INFO, "Debe seleccionar el nivel de dificultad de la digestion");
		    		showMessage(r);
		    	} else if (((FoodPojo)pojo).getName() == null) {
		    		Result r = new Result(Result.INFO, "Debe seleccionar la comida");
		    		showMessage(r);
		    	} else {
					
			        Result r = foods.save(pojo);
			        if (r.hasError()) {
			        	showMessage(r);
			        } else {
				    	finish();
			        }
		    	}
		    }
		});	        

		
		
        // COMIDAS RECIENTES
        Button btnAlimentacionRegistrarComidas = (Button) findViewById(R.id.btnAlimentacionRegistrarComidas);
        btnAlimentacionRegistrarComidas.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(AlimentacionComidasRecientes.class.getPackage().getName(), AlimentacionComidasRecientes.class.getName()));
				
         		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
         		parentActivity.startChildActivity("AlimentacionComidasRecientes", intent);
				
				//startActivity(intent);
		    }
		});	        

        // SELECCION FECHA Y HORA
        Button btnAlimentacionFecha = (Button) findViewById(R.id.btnAlimentacionFecha);
        btnAlimentacionFecha.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(SeleccionFechaHora.class.getPackage().getName(), SeleccionFechaHora.class.getName()));
				startActivity(intent);
		    }
		});	        

        
        // DIGESTION FACIL
        ImageButton btnAlimentacionFacil = (ImageButton) findViewById(R.id.imgbtnAlimentacionFacil);
        btnAlimentacionFacil.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	((FoodPojo)pojo).setDigestion( CrohnContext.FOOD_DIGESTION_ID_EASY);
		    	updateDigestionImages();
		    }
		});
        
        // DIGESTION MODERADA
        ImageButton btnAlimentacionModerada = (ImageButton) findViewById(R.id.imgbtnAlimentacionModerada);
        btnAlimentacionModerada.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	((FoodPojo)pojo).setDigestion( CrohnContext.FOOD_DIGESTION_ID_MODERATE);
		    	updateDigestionImages();
		    }
		});	        
        
        // DIGESTION DIFICIL
        ImageButton btnAlimentacionDificil = (ImageButton) findViewById(R.id.imgbtnAlimentacionDificil);
        btnAlimentacionDificil.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	((FoodPojo)pojo).setDigestion( CrohnContext.FOOD_DIGESTION_ID_HARD);
		    	updateDigestionImages();
		    }
		});	        
    
	}
    
    
    protected void updateDigestionImages() {
    	
    	digestion = ((FoodPojo)pojo).getDigestion();
    	
    	if (digestionAnterior != null){
    		if (digestionAnterior.equals(CrohnContext.FOOD_DIGESTION_ID_EASY))
    			((ImageButton) findViewById(R.id.imgbtnAlimentacionFacil)).setBackgroundResource(R.drawable.alimentacion_facil);
    		else if (digestionAnterior.equals(CrohnContext.FOOD_DIGESTION_ID_MODERATE))
    			((ImageButton) findViewById(R.id.imgbtnAlimentacionModerada)).setBackgroundResource(R.drawable.alimentacion_moderada);
    		else if (digestionAnterior.equals(CrohnContext.FOOD_DIGESTION_ID_HARD))
    			((ImageButton) findViewById(R.id.imgbtnAlimentacionDificil)).setBackgroundResource(R.drawable.alimentacion_dificil);

    	}
    	if (digestion != null){
    		try{
    		if (digestion.equals(CrohnContext.FOOD_DIGESTION_ID_EASY))
    			((ImageButton) findViewById(R.id.imgbtnAlimentacionFacil)).setBackgroundResource(R.drawable.alimentacion_facil_checked);
    		else if (digestion.equals(CrohnContext.FOOD_DIGESTION_ID_MODERATE))
    			((ImageButton) findViewById(R.id.imgbtnAlimentacionModerada)).setBackgroundResource(R.drawable.alimentacion_moderada_checked);
    		else if (digestion.equals(CrohnContext.FOOD_DIGESTION_ID_HARD))
    			((ImageButton) findViewById(R.id.imgbtnAlimentacionDificil)).setBackgroundResource(R.drawable.alimentacion_dificil_checked);
    		} catch(Exception e) {
    			Log.v("CROHN", "Exception: " + e.getMessage());
    		}
    	}
    	
    	digestionAnterior = digestion;
    }
    
}
